﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;

namespace E02
{
    public class A01 : ISYNChildGet
    {
        public void Code(ISYN handler, CodeResult result, SYNRight user, SerialType st, String baseNo, String billId)
        {
            try
            {
                SqlParameter[] sps = new SqlParameter[1];
                sps[0] = new SqlParameter("@FID", SqlDbType.NVarChar, 50);
                sps[0].Value = billId;

                StringBuilder sql = new StringBuilder();
                sql.Append("SELECT A.FID BillId,A.FBillNo BillNo,A.FComboBox BillStatus,A.FComboBox2 BillType,A.FNOTE BillNote,");
                sql.Append("A.FText CarNo,A.FText8 CardNo,A.FText4 BatchNo,A.FComboBox3 BeltLine,A.FInteger2 LoadTime,");
                sql.Append("A.FDecimal4 PlQty,A.FDecimal FiQty,A.FDecimal1 SeQty,A.FDecimal2 SuQty,A.FDecimal3 ToQty,");
                sql.Append("A.FTime PlTime,A.FTime1 FiTime,A.FTime2 SeTime,");
                sql.Append("A.FText1 PlEmp,A.FText2 FiEmp,A.FText3 SeEmp,A.FText9 ChEmp,");
                sql.Append("A.FText10 AutoCarNo,A.FCheckBox4 IsCancel,A.FText5 Stamp,");
                sql.Append("B.FItemID OrgId,B.FNumber OrgNo,B.FName OrgName,B.FHomePage OrgTh,");
                sql.Append("C.FID ItemId,C.FNumber ItemNumber,C.FName ItemName,C.FText ItemTh\r\n");
                sql.Append("FROM KEW_SalesInfo A LEFT JOIN t_Organization B ON A.FBase1=B.FItemID LEFT JOIN KEW_ProductsEntry C ON A.FBase=C.FID\r\n");
                sql.Append("WHERE A.FID=@FID \r\n");
                sql.Append("SELECT B.FID OrderId,B.FBillNo OrderNo,B.FText1 OrderTh,A.FClassID_SRC SourceType,");
                sql.Append("A.FText13 PricePolicy,A.FDecimal8 ItemTax,A.FDecimal13 ItemTaxPrice,A.FDecimal12 TranTax,A.FDecimal13 TranTaxPrice,A.FDecimal14 OthrPrice,A.FDecimal15 TotalPrice,");
                sql.Append("A.FDecimal9 DeductQty,A.FDecimal10 DeductAmount,A.FText6 Stamp\r\n");
                sql.Append("FROM KEW_SalesInfoEntry A LEFT JOIN KEW_SalesAccount B ON A.FID_SRC=B.FID\r\n");
                sql.Append("WHERE A.FID=@FID \r\n");

                Recordset rs = handler.Data.ExeProcedure(baseNo, sql.ToString(), sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows.Count == 2)
                    {
                        DataRow row = rs.Data.Tables[0].Rows[0];
                        E02A01M e02A01M = new E02A01M();
                        e02A01M.BillId = row["BillId"].ToString();
                        e02A01M.BillNo = row["BillNo"].ToString();
                        e02A01M.BillStatus = row["BillStatus"].ToString();
                        e02A01M.BillType = row["BillType"].ToString();
                        e02A01M.BillNote = row["BillNote"].ToString();
                        e02A01M.CarNo = row["CarNo"].ToString();
                        e02A01M.CardNo = row["CardNo"].ToString();
                        e02A01M.BatchNo = row["BatchNo"].ToString();
                        e02A01M.BeltLine = row["BeltLine"].ToString();
                        e02A01M.LoadTime = Common.CInt(row["LoadTime"].ToString());
                        e02A01M.PlQty = Common.Qty(row["PlQty"].ToString());
                        e02A01M.FiQty = Common.Qty(row["FiQty"].ToString());
                        e02A01M.SeQty = Common.Qty(row["SeQty"].ToString());
                        e02A01M.SuQty = Common.Qty(row["SuQty"].ToString());
                        e02A01M.ToQty = Common.Qty(row["ToQty"].ToString());
                        e02A01M.PlTime = row["PlTime"].ToString();
                        e02A01M.FiTime = row["FiTime"].ToString();
                        e02A01M.SeTime = row["SeTime"].ToString();
                        e02A01M.PlEmp = row["PlEmp"].ToString();
                        e02A01M.FiEmp = row["FiEmp"].ToString();
                        e02A01M.SeEmp = row["SeEmp"].ToString();
                        e02A01M.ChEmp = row["ChEmp"].ToString();
                        e02A01M.AutoCarNo = row["AutoCarNo"].ToString();
                        e02A01M.IsCancel = Common.CBool(row["IsCancel"].ToString());
                        e02A01M.Stamp = row["Stamp"].ToString();

                        foreach (DataRow entry in rs.Data.Tables[1].Rows)
                        { 
                            E02A01E e02A01E = new E02A01E();
                            e02A01E.OrderId = entry["OrderId"].ToString();
                            e02A01E.OrderNo = entry["OrderNo"].ToString();
                            e02A01E.OrderTh = entry["OrderTh"].ToString();
                            e02A01E.SourceType = entry["SourceType"].ToString();
                            e02A01E.PricePolicy = entry["PricePolicy"].ToString();
                            e02A01E.ItemTax = Common.CDec(entry["ItemTax"].ToString());
                            e02A01E.ItemTaxPrice = Common.Price(entry["ItemTaxPrice"].ToString());
                            e02A01E.TranTax = Common.CDec(entry["TranTax"].ToString());
                            e02A01E.TranTaxPrice = Common.Price(entry["TranTaxPrice"].ToString());
                            e02A01E.OthrPrice = Common.Price(entry["OthrPrice"].ToString());
                            e02A01E.TotalPrice = Common.Price(entry["TotalPrice"].ToString());
                            e02A01E.DeductQty = Common.Qty(entry["DeductQty"].ToString());
                            e02A01E.DeductAmount = Common.Amount(entry["DeductAmount"].ToString());
                            e02A01E.Stamp = entry["Stamp"].ToString();

                            e02A01M.OrderList.Add(e02A01E);
                        }
                    }
                    else
                    {
                        result.Result = EnumResult.None;
                    }
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = String.Format("销售计量单数据提取错误:{0}", rs.Message);
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = String.Format("销售计量单数据提取错误:{0}", ex.Message);
            }
        }
    }

    public class E02A01M
    {
        public String BillId = String.Empty; //单据ID
        public String BillNo = String.Empty; //单据编号
        public String BillStatus = String.Empty; //计量状态
        public String BillType = String.Empty; //计量类型
        public String BillNote = String.Empty; //备注
        public String CarNo = String.Empty; //车牌号
        public String CardNo = String.Empty; //磁卡编号
        public String BatchNo = String.Empty; //批次编号
        public String BeltLine = String.Empty; //生产线
        public int LoadTime = 0; //装货时间
        public Decimal PlQty = 0; //计划数量
        public Decimal FiQty = 0; //第一次计量数量
        public Decimal SeQty = 0; //第二次计量数量
        public Decimal SuQty = 0; //净重
        public Decimal ToQty = 0; //合计数量

        public String OrgId = String.Empty; //客户ID
        public String OrgNo = String.Empty; //客户编号
        public String OrgName = String.Empty; //客户名称
        public String OrgTh = String.Empty; //客户三方ID

        public String ItemId = String.Empty; //物料ID
        public String ItemNo = String.Empty; //物料编号
        public String ItemName = String.Empty; //物料名称
        public String ItemTh = String.Empty; //物料三方ID

        public String PlTime = String.Empty; //计划时间
        public String FiTime = String.Empty; //第一次计量时间
        public String SeTime = String.Empty; //第二次计量时间
        public String PlEmp = String.Empty; //计划录入人
        public String FiEmp = String.Empty; //第一次计量司磅人
        public String SeEmp = String.Empty; //第二次计量司磅人
        public String ChEmp = String.Empty; //签发人
        public String AutoCarNo = String.Empty; //自动识别的车牌号
        public Boolean IsCancel = false; //签发人
        public String Stamp = String.Empty; //更新时间戳

        public List<E02A01E> OrderList = new List<E02A01E>(); //订单
    }

    public class E02A01E
    { 
        public String OrderId = String.Empty; //订单ID
        public String OrderNo = String.Empty; //订单编号
        public String OrderTh = String.Empty; //订单三方ID
        public String SourceType = String.Empty; //源码类型
        public String PricePolicy = String.Empty; //价格政策
        public Decimal ItemTax = 0; //物料税率
        public Decimal ItemTaxPrice = 0; //物料含税单价
        public Decimal TranTax = 0; //运输税率
        public Decimal TranTaxPrice = 0; //运输含税单价
        public Decimal OthrPrice = 0; //其他单价
        public Decimal TotalPrice = 0; //合计单价
        public Decimal DeductQty = 0; //扣减数量
        public Decimal DeductAmount = 0; //扣减金额
        public String Stamp = String.Empty; //时间戳
    }
}
